package edu.cmich.cps.cps240.partyprincesspalace.hw06;

import java.awt.Rectangle;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;

/**
 * An input wire, which is treated as chip by the workspace for implementation
 * purposes.  This is our connection to "the outside world".  The input bus
 * has no input inside of the workspace itself, and provides the initial output
 * for the workspace to connect to other internal chips.
 * 
 * @author holde1ja
 */
public class InputBus extends Chip implements Serializable{
	
	private static final long serialVersionUID = 8817808804973103167L;
	//Our first InputBus is going to be created by the toolbox.  So the toolbox
	//will take InputBus #-1
	private static int ibcount = -1;


	/**
	 * Constructs a new input bus, with the given name, and with the location
	 * and shape defined by the given rectangle (just does a shallow copy of
	 * the rectangle)
	 * @param p_name The name that this input bus will have.
	 * @param p_rect The GUI representation of this 
	 */
	public InputBus(Rectangle p_rect)
	{
		super(p_rect, new Vector<ChipPin>(), new Vector<ChipPin>(), getNewName());
		ChipPin output = new ChipPin(name, this, null);
		outputs.add(output);
	}
	
	/**
	 * Calls the Chip copy constructor
	 * @param other
	 */
	public InputBus(InputBus other)
	{
		super(other);
		this.setName(getNewName());
	}
	
	
	/**
	 * @return A new name based on the number of InputBusses that have been created
	 */
	private static String getNewName(){
		String ret = "in" + ibcount;
		ibcount++;
		return ret;
	}
	
	
	/**
	 * Changes the name of the InputBus.  InputBuses, unlike normal Chips,
	 * can and SHOULD have a unique name
	 * @param p_name The new name for this InputBus
	 */
	public void setName(String p_name)
	{
		name = p_name;
		if (outputs.size() > 0 && outputs.get(0) != null && outputs.get(0).getPartner() != null)
		{
			((Wire)outputs.get(0).getPartner().getParent()).updateName(outputs.get(0));
		}
			
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.cmich.cps.cps240.partyprincesspalace.hw06.Chip#writeObject(java.io.ObjectOutputStream)
	 */
	public void writeObject(ObjectOutputStream out) throws IOException
	{
		out.defaultWriteObject(); 
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.cmich.cps.cps240.partyprincesspalace.hw06.Chip#readObject(java.io.ObjectInputStream)
	 */
	public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		// our "pseudo-constructor"
		in.defaultReadObject();
	
	}
}
